package cn.bestick.util;

/**
 * @author Bestick
 *
 *         be stick...
 *
 *         2015年9月7日 上午11:05:03
 */
public class PageUtil {

	public static String genPagation(String targetUrl, int totalNum,
			int currentPage, int pageSize) {
		//总页数
		int totalPage = totalNum % pageSize == 0 ? totalNum / pageSize
				: totalNum / pageSize + 1;
		StringBuffer pageCode = new StringBuffer();
		//首页
		pageCode.append("<li><a href='" + targetUrl + "?page=1'>首页</a></li>");
		//上一页逻辑
		if (currentPage==1) {
			pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
		}else {
			pageCode.append("<li><a href='"+targetUrl+"?page="+(currentPage - 1)+"'>上一页</a></li>");
		}
		//中间页逻辑
		for (int i = currentPage -2; i <= currentPage + 2; i++) {
			if (i<1||i>totalPage) {
				continue;
			}
			if (i == currentPage) {
				pageCode.append("<li class='active'><a href='#'>"+i+"</a></li>");
			}else {
				pageCode.append("<li><a href='"+targetUrl+"?page="+i+"'>"+i+"</a></li>");
			}
		}
		//下一页逻辑
		if (currentPage == totalPage) {
			pageCode.append("<li class='disabled'><a href='#'>下一页</a></li>");
		}else {
			pageCode.append("<li><a href='"+targetUrl+"?page="+(currentPage+1)+"'>下一页</a></li>");
		}
		//末页
		pageCode.append("<li><a href='"+targetUrl+"?page="+totalPage+"'>末页</a></li>");
		return pageCode.toString();
	}
}
